package controllers

import (
	"newAi/new_web/models"
	"fmt"
	"strconv"
	"strings"

	"github.com/astaxie/beego"
)

const (
	ProcessAction = 3
)

type AiImInfo struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data struct {
		Mine struct {
			Username string `json:"username"`
			ID       string `json:"id"`
			Status   string `json:"status"`
			Sign     string `json:"sign"`
			Avatar   string `json:"avatar"`
		} `json:"mine"`
		Friend `json:"friend"`
	} `json:"data"`
}

//好友分组
type Friend []struct {
	Groupname string `json:"groupname"`
	ID        int    `json:"id"`
	List      `json:"list"`
}

//好友列表
type List []struct {
	Username string `json:"username"`
	ID       string `json:"id"`
	Avatar   string `json:"avatar"`
	Sign     string `json:"sign"`
	Status   string `json:"status,omitempty"`
}

//下标 0：node_type 节点类型 1：scene 场景 2：mode 打断方式
var ProcessConfigArr = [...][10]string{
	{"语音文件", "TTS文字"},
	{"肯定", "否定", "强烈拒绝","关键节点", "未知", "短信", "挽留", "判断"},
	{"支持打断", "支持不打断", "支持按需求打断"},
	{"下一个", "跳转", "再说一遍", "挂断", "转人工", "挽留", "外部请求", "反问", "按键", "跳转流程"}}

type ProcessController struct {
	BaseController
}

func (self *ProcessController) List() {
	self.Data["pageTitle"] = "交互流程"
	Tpl_id, _ := self.GetInt("id", 0)
	//zoom, _ := self.GetInt("zoom", 0)
	self.Data["tpl_id"] = Tpl_id
	node_id,_ := self.GetInt64("node_id",0)
	if  node_id != 0 {
		self.Data["node_id"] = node_id
	}
	//self.Data["zoom"] = zoom
	self.display()
}

func (self *ProcessController) Globlalistview() {
	self.Data["pageTitle"] = "搜索结果"
	Tpl_id, _ := self.GetInt("id", 0)
	self.Data["tpl_id"] = Tpl_id
	self.display()
}

func (self *ProcessController) Searchview() {
	self.Data["pageTitle"] = "全局节点"
	Tpl_id, _ := self.GetInt("id", 0)
	self.Data["tpl_id"] = Tpl_id
	self.Data["content"] = self.GetString("content")
	self.Data["nodename"] = self.GetString("nodename")
	self.display()
}

func (self *ProcessController) Add() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("failed:", err)
		}
	}()
	self.Data["pageTitle"] = "新增交互流程"
	Tpl_id, _ := self.GetInt64("tpl_id", 0)
	parent_id, _ := self.GetInt64("id", 0)
	node, _ := self.GetInt64("node", 0)
	self.Data["tpl_id"] = Tpl_id
	self.Data["parent_id"] = parent_id
	self.Data["node"] = node
	if node != -1 { //如果不是 全局节点进来的， 就加一，显示当前下一个节点
		self.Data["node"] = node + 1
	}
	self.Data["global"], _ = self.GetInt("global")

	arr, _ := models.ProcessGetNodeArr(Tpl_id, "node")

	if len(arr) > 0 {
		var a = arr[len(arr)-1].Node
		nodeArr := make([][]models.AiProcess, a+1)
		for i := 0; i < len(arr); i++ {
			fmt.Println(i)
			if arr[i].Node != -1 && arr[i].Node != -2 && arr[i].Node != -3 && arr[i].Node != -5 && arr[i].Node != -6 {
				nodeArr[arr[i].Node] = append(nodeArr[arr[i].Node], *arr[i])
			}
		}

		self.Data["target"] = nodeArr
	}
	tpl, _ := models.TplGetById(int(Tpl_id))
	if tpl.Root_id == 0 {
		self.Data["all_tpl"], _ = models.GetAllTpl(self.userId, tpl.Id)
	} else {
		self.Data["all_tpl"], _ = models.GetAllTpl(self.userId, tpl.Root_id)
	}
	self.display()
}

func (self *ProcessController) Edit() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("failed:", err)
		}
	}()
	self.Data["pageTitle"] = "编辑交互流程"

	id, _ := self.GetInt("id", 0)
	process, _ := models.ProcessGetById(id)
	row := make(map[string]interface{})
	row["id"] = process.Id
	row["order"] = process.Ai_order
	row["name"] = process.Name
	row["node"] = process.Node
	row["process_node"] = process.Node_name
	row["content"] = process.Node_content
	row["keyword"] = process.Keyword
	row["owner_tpl_id"] = process.Owner_tpl_id
	row["node_type"] = process.Node_type
	row["scene"] = process.Scene
	row["mode"] = process.Mode
	row["target"] = process.Target
	row["node_desc"] = process.Node_desc
	row["action"] = process.Action
	row["min_speak_ms"] = process.Min_speak_ms
	row["min_pause_ms"] = process.Min_pause_ms
	row["max_waiting_ms"] = process.Max_waiting_ms
	row["max_recording_ms"] = process.Max_recording_ms
	row["retry"] = process.Retry
	row["gain"] = process.Gain
	row["parent_id"] = process.Parent_id
	row["b_interrupt"] = process.B_interrupt
	row["tpl_target"] = process.Tpl_target
	row["adjust_keyword"] = process.Adjust_keyword
	if int(process.Parent_id) != 0{
		parent, _ := models.ProcessGetById(int(process.Parent_id))
		row["parent"] = parent.Node_desc
	}



	self.Data["process"] = row
	tpl_id, _ := self.GetInt("tpl_id", 0)
	self.Data["tpl_id"] = tpl_id

	fmt.Println(self.GetString("edit"))
	self.Data["isGlobla"] = self.GetString("edit")
	self.Data["searchview"], _ = self.GetBool("searchview")

	tplid := process.Owner_tpl_id
	nodeArr := sortTaskNodeByTplId(tplid) //根据tpl_id 进行 task的排序
	self.Data["target"] = nodeArr
	self.Data["isGlobal"] = true
	if process.B_interrupt || process.Node == -1 {
		self.Data["isGlobal"] = false
	}
	if process.Node != -1 && process.Node != -2 && process.Node != -3 {
		self.Data["target1"] = nodeArr[process.Node]
	}
	tpl, _ := models.TplGetById(int(process.Owner_tpl_id))
	if tpl.Root_id == 0 {
		self.Data["all_tpl"], _ = models.GetAllTpl(self.userId, tpl.Id)
	} else {
		self.Data["all_tpl"], _ = models.GetAllTpl(self.userId, tpl.Root_id)
	}
	self.display()
}

func (self *ProcessController) EditData() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("failed:", err)
		}
	}()
	self.Data["pageTitle"] = "编辑交互流程"

	id, _ := self.GetInt("id", 0)
	process, _ := models.ProcessGetById(id)
	row := make(map[string]interface{})
	row["id"] = process.Id
	row["order"] = process.Ai_order
	row["name"] = process.Name
	row["node"] = process.Node
	row["process_node"] = process.Node_name
	row["content"] = process.Node_content
	row["keyword"] = process.Keyword
	row["owner_tpl_id"] = process.Owner_tpl_id
	row["node_type"] = process.Node_type
	row["scene"] = process.Scene
	row["mode"] = process.Mode
	row["target"] = process.Target
	row["node_desc"] = process.Node_desc
	row["action"] = process.Action
	row["min_speak_ms"] = process.Min_speak_ms
	row["min_pause_ms"] = process.Min_pause_ms
	row["max_waiting_ms"] = process.Max_waiting_ms
	row["max_recording_ms"] = process.Max_recording_ms
	row["retry"] = process.Retry
	row["gain"] = process.Gain
	row["parent_id"] = process.Parent_id
	row["b_interrupt"] = process.B_interrupt
	row["tpl_target"] = process.Tpl_target
	row["adjust_keyword"] = process.Adjust_keyword
	if int(process.Target) != 0{
		parent, _ := models.ProcessGetById(int(process.Target))
		row["target_name"] = parent.Node_name
	}

	self.ajaxJson(row, MSG_OK)
}

//根据tpl_id 进行 task的排序
func sortTaskNodeByTplId(tplid int64) [][]models.AiProcess {
	arr, _ := models.ProcessGetNodeArr(tplid, "node")

	nodeArr := make([][]models.AiProcess, arr[len(arr)-1].Node+1)

	for i := 0; i < len(arr); i++ {
		if arr[i].Node != -1 && arr[i].Node != -2 && arr[i].Node != -3 && arr[i].Node != -5 && arr[i].Node != -6 {
			nodeArr[arr[i].Node] = append(nodeArr[arr[i].Node], *arr[i])
		}
	}
	return nodeArr
}

func (self *ProcessController) GetSortTask() {
	tpl_id, _ := self.GetInt64("id")
	self.ajaxJson(sortTaskNodeByTplId(tpl_id), MSG_OK)
}

//节点内容
func (self *ProcessController) Content() {
	self.Data["pageTitle"] = "节点内容"

	id, _ := self.GetInt("id", 0)
	process, _ := models.ProcessGetById(id)
	row := make(map[string]interface{})
	row["id"] = process.Id

	self.Data["process"] = row
	self.display()
}

//全局变量
func (self *ProcessController) AddGlobal() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("failed:", err)
		}
	}()
	self.Data["pageTitle"] = "配置不匹配节点"
	self.Data["title"] = self.GetString("pageTitle")
	id, _ := self.GetInt64("id")
	parent_id, _ := self.GetInt64("parent_id")
	var node = -3
	if self.GetString("pageTitle") == "静音内容" {
		self.Data["pageTitle"] = "配置静音节点"
		node = -2
		if parent_id == 0 { //如果是全局 静音
			node = -5
		}
	} else if parent_id == 0 { //如果 是全局不匹配
		node = -6
	}
	fmt.Println("node:", node)
	process, _ := models.ProcessGetByTplId(id, node, parent_id)
	row := make(map[string]interface{})
	row["id"] = process.Id
	row["order"] = process.Ai_order
	row["name"] = process.Name
	row["node"] = process.Node
	row["process_node"] = process.Node_name
	row["content"] = process.Node_content
	row["keyword"] = process.Keyword
	row["owner_tpl_id"] = process.Owner_tpl_id
	row["node_type"] = process.Node_type
	row["scene"] = process.Scene
	row["mode"] = process.Mode
	row["target"] = process.Target
	row["node_desc"] = process.Node_desc
	row["min_speak_ms"] = process.Min_speak_ms
	row["min_pause_ms"] = process.Min_pause_ms
	row["max_waiting_ms"] = process.Max_waiting_ms
	row["max_recording_ms"] = process.Max_recording_ms
	row["retry"] = process.Retry
	row["gain"] = process.Gain
	row["parent_id"] = process.Parent_id
	row["adjust_keyword"] = process.Adjust_keyword
	audio, _ := models.ProcessContentByProcess(process.Id)
	row["audio_path"] = audio.Audio_path
	self.Data["p"] = row

	self.display()
}

//节点全局列表
func (self *ProcessController) Tablelist() {
	parent_id, _ := self.GetInt64("parent_id")
	if parent_id != 0 {
		self.ajaxList("成功", MSG_OK, 200, "啊啊")
	}
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit
	//查询条件
	filters := make([]interface{}, 0)
	tpl_id, _ := self.GetInt("id")
	content := self.GetString("content")
	nodename := self.GetString("nodename")
	if content == "" {
		filters = append(filters, "owner_tpl_id", tpl_id, "Node__gt", -2, "node", -1)
	} else {
		filters = append(filters, "owner_tpl_id", tpl_id, "Node__gt", -2, nodename+"__icontains", content)
	}
	result, count := models.ProcessGetList(page, self.pageSize, "process", filters...)

	list := make([]map[string]interface{}, len(result)) //列表
	//列表
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["order"] = v.Ai_order
		row["name"] = v.Name
		row["node_name"] = v.Node_name
		row["node_content"] = v.Node_content
		row["node_type"] = ProcessConfigArr[0][v.Node_type]
		row["owner_tpl_id"] = v.Owner_tpl_id
		row["node_desc"] = v.Node_desc
		row["Scene"] = ProcessConfigArr[1][v.Scene]
		row["mode"] = ProcessConfigArr[2][v.Mode]
		row["keyword"] = v.Keyword
		row["action"] = ProcessConfigArr[3][v.Action]
		row["node"] = v.Node
		if v.Node == -1 {
			row["node"] = "全局节点"
		}
		if v.Target == 0 { //如果一开始没有目标 就设置为 空
			row["target"] = ""
		} else {
			target, err := models.ProcessGetById(int(v.Target))
			if err == nil {
				row["target"] = target.Node_name
			} else {
				row["target"] = ""
			}
		}
		list[k] = row

	}
	self.ajaxList("成功", MSG_OK, count, list)
}

//节点内容
func (self *ProcessController) AddContent() {
	self.Data["pageTitle"] = "添加节点内容"
	self.Data["id"], _ = self.GetInt("id")
	self.display()
}

//节点内容
func (self *ProcessController) EditContent() {
	self.Data["pageTitle"] = "编辑节点内容"

	id, _ := self.GetInt("id")
	content, _ := models.ProcessContentGetById(id)

	self.Data["id"] = content.Id
	self.Data["process_id"] = content.Process_id
	self.Data["content"] = content.Content
	self.Data["type"] = content.Type
	self.Data["order"] = content.Ai_order
	self.Data["audio_path"] = content.Audio_path
	self.Data["sid"], _ = self.GetInt("sid")

	self.display()
}

//节点内容
func (self *ProcessController) ContentTable() {

	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit
	process_id, _ := self.GetInt("id")
	//查询条件
	filters := make([]interface{}, 0)
	filters = append(filters, "process_id", process_id)
	result, count := models.ProcessContentGetList(page, self.pageSize, "process_content", filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["order"] = v.Ai_order
		row["type"] = ProcessConfigArr[0][int(v.Type)]
		row["content"] = v.Content
		row["audio_path"] = v.Audio_path

		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}

//节点内容
func (self *ProcessController) GetProcessContent() {

	process_id, _ := self.GetInt("id")
	//查询条件
	filters := make([]interface{}, 0)
	filters = append(filters, "process_id", process_id)
	result, count := models.ProcessContentGetList(-1, -1, "process_content", filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["order"] = v.Ai_order
		row["type"] = ProcessConfigArr[0][int(v.Type)]
		row["content"] = v.Content

		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}

type OrgData struct {
	Node        int       `json:"node"`
	Name        string    `json:"name"`
	Children    []OrgData `json:"children"`
	Title       string    `json:"title"`
	Id          int64     `json:"id"`
	B_interrupt bool      `json:"b_interrupt"`
	ClassName   string    `json:"className"`
	Node_desc   string    `json:"node_desc"`
	Scene       int    `json:"scene"`
}

//列表
func (self *ProcessController) Table() {
	parent_id, _ := self.GetInt64("parent_id")
	node_id, _ := self.GetInt64("node_id")//不匹配进来的node_id ,如果有这个id的话，需要把那个节点高亮
	if parent_id != 0 {
		self.ajaxList("成功", MSG_OK, 200, "啊啊")
	}
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit
	//查询条件
	filters := make([]interface{}, 0)
	tpl_id, _ := self.GetInt("id")
	filters = append(filters, "owner_tpl_id", tpl_id, "Node", 0)
	result, count := models.ProcessGetList(page, self.pageSize, "process", filters...)
	//list := make([]map[string]interface{}, len(result)) //列表
	var node int64
	//列表
	for _, v := range result {
		if v.Node == 0 {
			node = v.Id
		}
	} //列表
	var orgData = orgInfo(tpl_id, 1, node,node_id)
	self.ajaxList("成功", MSG_OK, count, orgData)

}

func orgInfo(tpl int, node int, parent, not_node_id int64) OrgData {
	var id, err = models.ProcessGetChildArr(tpl, node, parent)
	if err != nil {
		beego.Error(err)
	}
	var org OrgData

	for _, v := range id {
		var childorg = orgInfo(tpl, node+1, v.Id, not_node_id) //节点id变大
		process, err := models.ProcessGetById(int(v.Id))
		if err != nil {
			beego.Error(err)
		}
		//添加节点上面的字段
		childorg.Node = process.Node
		if process.B_interrupt {
			childorg.Name = "打断" + strconv.Itoa(process.Node) + ":" + process.Node_name
			childorg.ClassName = "aa-level"
		} else {
			childorg.Name = "节点" + strconv.Itoa(process.Node) + ":" + process.Node_name
			if process.Scene == 6 {
				childorg.ClassName = "bb-level"
			}
		}
		if not_node_id == process.Id{//选中的节点
			childorg.ClassName = "cc-level"
		}
		childorg.Title = process.Keyword
		childorg.Id = process.Id
		childorg.B_interrupt = process.B_interrupt
		childorg.Node_desc = process.Node_desc
		childorg.Scene = process.Scene
		org.Children = append(org.Children, childorg)
	}
	process, _ := models.ProcessGetById(int(parent))
	if not_node_id == process.Id{//选中的节点
		org.ClassName = "cc-level"
	}
	org.Name = "节点" + strconv.Itoa(process.Node) + ":" + process.Node_name
	org.Id = process.Id
	org.Node_desc = process.Node_desc

	return org
}

//保存数据库
func (self *ProcessController) AjaxSave() {
	//
	Process := new(models.AiProcess)

	Process.Name = strings.TrimSpace(self.GetString("process_name"))            //流程名称
	Process.Node, _ = self.GetInt("node")                                       //任务节点
	Process.Node_name = strings.TrimSpace(self.GetString("process_node"))       //节点名称
	Process.Node_type, _ = self.GetInt("node_type")                             //节点类型，0为语音文件，1为TTS文字
	Process.Node_content = strings.TrimSpace(self.GetString("process_content")) //节点内容，根据类型，文字或者录音文件绝对地址
	Process.Keyword = strings.TrimSpace(self.GetString("process_keyword"))      //关键字

	Process.Owner_tpl_id, _ = self.GetInt64("tpl_id") //归属模板id
	Process.Scene, _ = self.GetInt("scene")           //场景；语境；情景；0为肯定，1为否定，2为强烈拒绝，3为未知
	Process.Ai_order, _ = self.GetInt("order")        //节点优先关系处理
	Process.Mode, _ = self.GetInt("mode")
	if i, _ := self.GetInt64("action"); i == 0 {
		Process.Target, _ = self.GetInt64("target")
	} else if i == 1 || i == 9 {
		Process.Target, _ = self.GetInt64("target1")
	}
	Process.Node_desc = self.GetString("node_desc") //节点描述
	Process.Action, _ = self.GetInt64("action")
	Process.Min_speak_ms, _ = self.GetInt("min_speak_ms")
	Process.Min_pause_ms, _ = self.GetInt("min_pause_ms")
	Process.Max_waiting_ms, _ = self.GetInt("max_waiting_ms")
	Process.Max_recording_ms, _ = self.GetInt("max_recording_ms")
	Process.Gain, _ = self.GetInt("gain")
	Process.Retry, _ = self.GetInt("retry")
	Process.Parent_id, _ = self.GetInt64("parent_id")
	Process.B_interrupt, _ = self.GetBool("anxu")
	Process.Adjust_keyword = self.GetString("adjust_keyword")
	Process.Tpl_target, _ = self.GetInt64("tpl_target")
	if i, _ := self.GetInt64("action"); i != 9 {
		Process.Tpl_target = 0
	}
	//Process
	//打断。0支持打断，1支持不打断，2支持按需求打断

	//判断 是否存在 ID， 如果有ID的话，就进行update
	if i, _ := self.GetInt64("id"); i != 0 {
		Process.Id = i
		if err := Process.Update(); err != nil {
			self.ajaxMsg(err.Error(), MSG_ERR)
		}
		self.ajaxMsg("", MSG_OK)
	}

	//如果没有ID  ，就是新增的
	processinfo := make(map[string]int64)
	processid, err := models.ProcessAdd(Process)
	if err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	processinfo["id"] = processid
	processinfo["parent_id"], _ = self.GetInt64("parent_id")
	self.ajaxJson(processinfo, MSG_OK)

}

func (self *ProcessController) ProcessContentSave() {
	//self.ajaxMsg("分组名已经存在", MSG_ERR)

	//if self.GetInt64("process_id") == {
	//
	//}

	content := new(models.AiProcessContent)

	content.Content = strings.TrimSpace(self.GetString("content"))
	content.Process_id, _ = self.GetInt64("process_id")
	content.Type, _ = self.GetInt("type")
	content.Ai_order, _ = self.GetInt("order")
	if self.GetString("message") != " " {
		content.Audio_path = self.GetString("message")
	}

	//Process.Name = strings.TrimSpace(self.GetString("process_name"))            //流程名称

	////判断 是否存在 ID， 如果有ID的话，就进行update
	i, _ := self.GetInt64("id")
	if i != 0 {
		content.Id = i
		if err := content.Update(); err != nil {
			self.ajaxMsg(err.Error(), MSG_ERR)
		}
		self.ajaxMsg("", MSG_OK)
	}

	var err error
	_, err = models.ProcessContentAdd(content)
	if err != nil {
		fmt.Println(err)
		self.ajaxMsg(err.Error(), MSG_ERR)
	}

	self.ajaxMsg("", MSG_OK)
}

func (self *ProcessController) ProcessContentUpdate() {

	content := new(models.AiProcessContent)

	content.Content = strings.TrimSpace(self.GetString("process_content"))
	content.Process_id, _ = self.GetInt64("process_id")
	content.Type, _ = self.GetInt("node_type")
	content.Ai_order, _ = self.GetInt("order")
	content.Audio_path = self.GetString("message")

	err := content.ProcessContentByProcessId()
	if err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("", MSG_OK)

}

func (self *ProcessController) AjaxDel() {
	Process_id, _ := self.GetInt("id")
	Process, _ := models.ProcessGetById(Process_id)

	if err := Process.Delete(); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("", MSG_OK)
}

func (self *ProcessController) ContentDel() {
	Process_id, _ := self.GetInt("id")
	Process, _ := models.ProcessContentGetById(Process_id)

	if err := Process.Delete(); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("", MSG_OK)
}

func (self *ProcessController) UpdateKeyword() {
	Process_id, _ := self.GetInt64("id")
	keyword := self.GetString("keyword")
	err := models.UpdateKeywordFromId(keyword, Process_id)

	if err != nil {
		self.ajaxMsg(err, MSG_ERR)
	}
	self.ajaxMsg("成功", MSG_OK)
}

//上传语音文件
func (self *ProcessController) Upload() {
	path, err := self.UploadFile("/opt/fsgui/nway-sys/sounddir/", "uploadField")

	if err == nil {
		//models.ProcessContentAdd()
		self.ajaxMsg(path, MSG_OK)
	} else {
		self.ErrorLog(err)
	}

}

//获取 即时通讯的 message
func (self *ProcessController) GetImMessage() {

	message := self.GetString("message")

	tpl_id, _ := self.GetInt64("tpl_id")
	node, _ := self.GetInt("node")

	//result, err := models.ProcessToNodeForMessage(message, node, tpl_id)

	result, err := models.ProcessToNodeForKeyword(node, tpl_id)
	if err != nil {
		self.ajaxMsg(message, MSG_ERR)
	}

	index := -2
Exit:
	for k, v := range result {
		cmds := strings.Split(v.Keyword, "|")
		for _, v1 := range cmds {
			if strings.Contains(message, v1) {
				index = k
				break Exit
			}
		}
	}

	if index != -2 {
		if result[index].Action == 1 {
			result[index], err = models.ProcessGetById(int(result[index].Target))
			self.ajaxMessage(result[index].Node_content, result[index].Node, ProcessAction)
		}
		self.ajaxMsg(result[index].Node_content, MSG_OK)
	} else {
		self.ajaxMsg(message, MSG_ERR)
	}
}

//拖拽节点，修改节点的位置
func (self *ProcessController) SetNodePosition() {
	id, _ := self.GetInt64("id")
	dropZone, _ := self.GetInt64("dropZone")
	err := models.SetNodePositionMode(id, dropZone)
	if err != nil {
		self.ajaxMsg(err, MSG_ERR)
	}
	self.ajaxMsg("修改成功", MSG_OK)
}

//复制节点
func (self *ProcessController) CopyNodePercess() {
	id, _ := self.GetInt64("id")
	models.CopyNodeProcess(id)
	self.ajaxMsg("修改成功", MSG_OK)
}

//获取 开场白message
func (self *ProcessController) GetImNodeMessage() {

	tpl_id, _ := self.GetInt64("tpl_id")
	node, _ := self.GetInt("node")
	result, err := models.GetNodeProcessMessage(node, tpl_id)

	if err != nil {
		self.ajaxMsg(err, MSG_ERR)
	}

	self.ajaxMsg(result.Node_content, MSG_OK)
}

//获取 开场白message
func (self *ProcessController) SetMute() {

	//
	Process := new(models.AiProcess)

	//Process.Name = " "
	Process.Node, _ = self.GetInt("node") //任务节点
	//Process.Node_name = strings.TrimSpace(self.GetString("process_node"))       //节点名称
	Process.Node_type, _ = self.GetInt("node_type")                             //节点类型，0为语音文件，1为TTS文字
	Process.Node_content = strings.TrimSpace(self.GetString("process_content")) //节点内容，根据类型，文字或者录音文件绝对地址
	//Process.Keyword = strings.TrimSpace(self.GetString("process_keyword"))      //关键字

	Process.Owner_tpl_id, _ = self.GetInt64("tpl_id") //归属模板id
	//Process.Scene, _ = self.GetInt("scene")           //场景；语境；情景；0为肯定，1为否定，2为强烈拒绝，3为未知
	//Process.Ai_order, _ = self.GetInt("order")        //节点优先关系处理
	Process.Mode, _ = self.GetInt("mode")
	//if i, _ := self.GetInt64("action"); i == 0 {
	//	Process.Target, _ = self.GetInt64("target")
	//} else if i == 1 {
	//	Process.Target, _ = self.GetInt64("target1")
	//}
	//Process.Node_desc = self.GetString("node_desc") //节点描述
	//Process.Action, _ = self.GetInt64("action")
	Process.Min_speak_ms, _ = self.GetInt("min_speak_ms")
	Process.Min_pause_ms, _ = self.GetInt("min_pause_ms")
	Process.Max_waiting_ms, _ = self.GetInt("max_waiting_ms")
	Process.Max_recording_ms, _ = self.GetInt("max_recording_ms")
	Process.Gain, _ = self.GetInt("gain")
	Process.Retry, _ = self.GetInt("retry")
	//Process
	//打断。0支持打断，1支持不打断，2支持按需求打断
	Process.Id, _ = self.GetInt64("id")
	if err := Process.Update(); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg(" 成功", MSG_OK)

}

//获取 即时通讯的 信息
func (self *ProcessController) GetImListInfo() {
	list := new(AiImInfo)
	//查询条件
	filters := make([]interface{}, 0)
	tpl, err := models.GetImTplList("tpl", filters...)
	if err != nil {
		self.ajaxMsg(err, MSG_ERR)
	}
	//
	list.Code = 0
	list.Msg = ""
	mine := list.Data.Mine
	mine.Username = beego.AppConfig.String("site.name") //im名字
	mine.Sign = beego.AppConfig.String("site.sign")     //签名
	mine.Avatar = beego.AppConfig.String("site.avatar") //头像
	mine.ID = "100000"                                  //id
	mine.Status = "online"                              //是否在线
	list.Data.Mine = mine

	//一共有多少好友
	lists := make(List, len(tpl))

	for i, v := range tpl {
		id := strconv.FormatInt(v.Id, 10)
		lists[i].ID = id
		lists[i].Avatar = "/static/img/userface.jpg"
		lists[i].Username = v.Tpl_name
		lists[i].Sign = v.Tpl_desc
		lists[i].Status = "online"
	}
	//im 分组 列表
	friend := Friend{
		{
			ID:        10000,
			Groupname: "话术模板",
			List:      lists,
		},
	}
	list.Data.Friend = friend

	self.ajaxJson(list, MSG_OK)

}
